package com.joysuch.wwyt.workflow.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.joysuch.wwyt.workflow.WorkFlowConstants;
import com.joysuch.wwyt.workflow.bean.WorkFlowExecuteTaskBean;
import com.joysuch.wwyt.workflow.bean.WorkFlowTaskCreateBean;
import com.joysuch.wwyt.workflow.bean.WorkFlowTaskQueryBean;
import com.joysuch.wwyt.workflow.entity.WorkFlowProcess;
import com.joysuch.wwyt.workflow.entity.WorkFlowTask;
import com.joysuch.wwyt.workflow.service.WorkFlowHiddenDangerService;
import com.joysuch.wwyt.workflow.service.WorkFlowProcessService;
import com.joysuch.wwyt.workflow.service.WorkFlowTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Slf4j
public class WorkFlowHiddenDangerServiceImpl implements WorkFlowHiddenDangerService {

    @Autowired
    private WorkFlowTaskService workFlowTaskService;

    @Autowired
    private WorkFlowProcessService workFlowProcessService;


    /**
     * APP 上传任务
     * @param task
     */

    @Override
    public void uploadTask(WorkFlowTask task){

        Map<String, Object> varMap =  mapStringToMap(task.getVarMapData());

        if(WorkFlowConstants.APP_TASK_NAME_ACCEPT.equalsIgnoreCase(task.getTaskName())
        || WorkFlowConstants.APP_TASK_NAME_CHECK.equalsIgnoreCase(task.getTaskName())){
            //整改验收与隐患核销直接提交任务到 流程实例

            WorkFlowExecuteTaskBean taskBean = new WorkFlowExecuteTaskBean();
            taskBean.setId(task.getId());
            taskBean.setTaskName(task.getTaskName());
            taskBean.setTaskId(task.getTaskId());
            taskBean.setFormData(task.getFormData());
            taskBean.setWorkFlowProcessId(task.getProcessId());
            taskBean.setVarMap(varMap);
            taskBean.setAppStatus(WorkFlowConstants.APP_TASK_STATUS_UPLOAD_TASK);


          //  workFlowTaskService.submitTask(taskBean, re);

        } else if(WorkFlowConstants.APP_TASK_NAME_RECTIFY.equalsIgnoreCase(task.getTaskName())){
            //在巡检点下达整改单： 直接创建新任务流程实例
            WorkFlowTaskCreateBean taskCreateBean = new WorkFlowTaskCreateBean();
            taskCreateBean.setFormData(task.getFormData());
            taskCreateBean.setTaskName(task.getTaskName());
            taskCreateBean.setVarMap(varMap);
            taskCreateBean.setAppStatus(WorkFlowConstants.APP_TASK_STATUS_UPLOAD_TASK);
            //获取隐患管理的流程ID
            WorkFlowProcess workFlowProcess = workFlowProcessService.findByBizName(WorkFlowConstants.APP_WORKFLOW_HIDDEN_DANGER);
            taskCreateBean.setWorkFlowId(workFlowProcess.getId());
          //  workFlowTaskService.createNewTask(taskCreateBean);
        }
    }

    public static Map<String,Object> mapStringToMap(String str){
        log.info("... varMapData= "+ str);
        if(str == null || str.length() <= 2){
            return new HashMap<>();
        }
        str=str.substring(1, str.length()-1);
        String[] strs=str.split(",");
        Map<String,Object> map = new HashMap<String, Object>();
        for (String string : strs) {
            String key=string.split("=")[0];
            String value=string.split("=")[1];
            map.put(key, value);
        }
        return map;
    }

    @Override
    public List<WorkFlowTask> downloadTaskList(String taskName) {
        WorkFlowProcess workFlowProcess = workFlowProcessService.findByBizName(WorkFlowConstants.APP_WORKFLOW_HIDDEN_DANGER);
        WorkFlowTaskQueryBean queryBean = new WorkFlowTaskQueryBean();

        queryBean.setWorkFlowProcessId(workFlowProcess.getId());
        List<String> taskNameList = new ArrayList<>();
        taskNameList.add(taskName);
        queryBean.setTaskName(taskNameList);
        return (List<WorkFlowTask>) workFlowTaskService.findTaskList(queryBean, null).getData();


    }

    @Override
    public List<WorkFlowTask> getPendTaskList(List<String> taskNameList) {
        WorkFlowProcess workFlowProcess = workFlowProcessService.findByBizName(WorkFlowConstants.APP_WORKFLOW_HIDDEN_DANGER);
        WorkFlowTaskQueryBean queryBean = new WorkFlowTaskQueryBean();

        queryBean.setWorkFlowProcessId(workFlowProcess.getId());
        queryBean.setTaskName(taskNameList);
        return (List<WorkFlowTask>) workFlowTaskService.findTaskList(queryBean, null).getData();
    }
}
